package com.haosheng.testproject.ui;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.haosheng.testproject.R;
import com.haosheng.testproject.utils.App;
import com.haosheng.testproject.utils.HttpSuccess;
import com.haosheng.testproject.utils.HttpUtils;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * baseActivity
 */

public abstract class BaseActivity extends AppCompatActivity {
    
    private TextView mTvError;
    private FrameLayout mFrameContent;
    private FrameLayout mContainer;
    private ProgressBar mProgressBar;
    private FrameLayout mFrameLayoutContent;
    private Toast mToast;
    private App mApp;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentView());
        initOther();
        initView();
        initEvent();
        next();
    }
    
    public void initOther() {
        mApp = (App) getApplication();
    }
    
    private void initEvent() {
        mTvError.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toggleContentVisible();
            }
        });
    }
    
    protected abstract void next();
    
    /**
     * 加载视图
     */
    private void initView() {
        mTvError = (TextView) findViewById(R.id.tv_error);
        mFrameContent = (FrameLayout) findViewById(R.id.frame_layout_content);
        mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
        
        mTvError.setVisibility(View.INVISIBLE);
        mProgressBar.setVisibility(View.INVISIBLE);
        
        mFrameContent.addView(getContent());
    }
    
    public int getContentView() {
        return R.layout.activity_main;
    }
    
    /**
     * 设置内容的东西
     */
    public abstract View getContent();
    
    /**
     * 设置内容View的显示和隐藏
     */
    public void setContentVisible(boolean b) {
        
        if (b) {
            //显示
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mFrameContent.setVisibility(View.VISIBLE);
                    mTvError.setVisibility(View.GONE);
                }
            });
        } else {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mFrameContent.setVisibility(View.GONE);
                    mTvError.setVisibility(View.VISIBLE);
                }
            });
        }
    }
    
    /**
     * 转换内容显示的状态
     */
    public void toggleContentVisible() {
        if (mFrameContent.getVisibility() == View.VISIBLE) {
            mFrameContent.setVisibility(View.GONE);
            mTvError.setVisibility(View.VISIBLE);
        } else {
            mFrameContent.setVisibility(View.VISIBLE);
            mTvError.setVisibility(View.GONE);
        }
    }
    
    /**
     * 加载中
     */
    public void loadingShow() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mProgressBar.setVisibility(View.VISIBLE);
            }
        });
    }
    
    /**
     * 加载中隐藏
     */
    public void loadingHide() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mProgressBar.setVisibility(View.GONE);
            }
        });
    }
    
    /**
     * get请求
     */
    public void httpGet(String url, final HttpSuccess httpSuccess) {
        loadingShow();
        HttpUtils.getInstance().httpGet(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                loadingHide();
                setContentVisible(false);
            }
            
            @Override
            public void onResponse(Call call, final Response response) throws IOException {
                loadingHide();
                setContentVisible(true);
                httpSuccess.httpSuccess(response.body().string());
            }
        });
    }
    
    /**
     * 打印log
     */
    public void dLog(String str) {
        if (App.IS_DEBUG) {
            if (str != null)
                Log.d(App.APP_NAME, str);
            else
                Log.d(App.APP_NAME, "字符串为空");
        }
    }
    
    /**
     * 吐司
     */
    public void toast(String str) {
        if (mToast == null) {
            mToast = new Toast(this);
            mToast.setDuration(Toast.LENGTH_SHORT);
        }
        mToast.setText(str);
        mToast.show();
    }
    
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
